package com.pbd.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AlbumViewer extends VerticalPanel {

	private VerticalPanel viewPanel;
	private FlexTable mainPanel;
//	private FlexTable thumbnailGrid;
	private Grid albumViewGrid;
//	private HorizontalPanel namePanel;
	private Long albumId;

	public AlbumViewer(Long albumId) {
		super();
		this.albumId = albumId;
		initComponents();

	}

	private void initComponents() {

		System.err.println("Inside Init Components Method");
		


		mainPanel = new FlexTable();
		// mainPanel.setStyleName("table-bordered");
		//mainPanel.setStylePrimaryName("roundedPanel");
		//mainPanel.setStyleName("roundedTable");

		mainPanel.setBorderWidth(1);
		mainPanel.setCellPadding(3);

		mainPanel.setWidth("660px");
		add(mainPanel);
		createImageShowPanel();
//		int noOfAlbum = albumMasterList == null ? 0 : albumMasterList.size();

//		if (noOfAlbum > 0) {}
	
	}

	protected void createImageShowPanel() {

		ServiceUtil.getHibernateServiceAsync().findObjects("MEDIARELATION", new String[] {"album.layout", "mediaSource", "mediaDesc" }, "MEDIARELATION.album.albumId=" + this.albumId, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace(System.err);
			}

			@Override
			public void onSuccess(Object result) {
				List<ArrayList> mediaList = (List<ArrayList>) result;

				int size = mediaList.size();

				int rows = size % 4;
				if (rows == 0) {
					rows = size / 4;
				}

				viewPanel = new VerticalPanel();

				albumViewGrid = new Grid(rows, 4);
				albumViewGrid.setCellPadding(5);
				albumViewGrid.setCellSpacing(5);


				viewPanel.add(albumViewGrid);

				mainPanel.setWidget(0, 0, viewPanel);
				mainPanel.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);

				int column = 0;

				int rowValue = 0;

				for (Iterator<ArrayList> iterator = mediaList.iterator(); iterator.hasNext();) {

					ArrayList mediaValueList = iterator.next();

					if (mediaValueList != null && mediaValueList.size() > 0) {

						final String mediaSource = (String) mediaValueList.get(1);
						final String descriptionPath = (String) mediaValueList.get(2);

						if (mediaSource != null && mediaSource.trim().length() > 0) {

							Image image = new Image("." + mediaSource);

							//image.setStyleName("roundedPanel");

							image.setPixelSize(150, 150);

							albumViewGrid.setWidget(rowValue, column, image);

							if (column == 3) {
								column = 0;
								if (rowValue < rows) {
									rowValue++;
								}
							} else 
								column++;
							

							image.addClickHandler(new ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									PopupPanel popup = new PopupPanel(true);
									//popup.setStyleName("modal");
									Image img = new Image("." + mediaSource);
									img.setWidth("100%");
									img.setHeight("100%");
//									img.setPixelSize(300, 300);
									popup.add(img);
									popup.setGlassEnabled(true);
									popup.center();
									popup.show();
								}
							});
						}else{
							String moduleBaseURL = GWT.getModuleBaseURL();
							
							RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, moduleBaseURL+"FileUploadService");

							rb.setHeader("descpath", descriptionPath);
							try {
								rb.sendRequest("", new RequestCallback() {

									@Override
									public void onResponseReceived(Request request, Response response) {
										HTML html = new HTML(response.getText());
										viewPanel.clear();
										viewPanel.add(html);

									}

									@Override
									public void onError(Request request, Throwable exception) {
										exception.printStackTrace(System.err);

									}
								});

							} catch (RequestException e) {
								e.printStackTrace();
							}
							
							//HTMLPanel html = new HTMLPanel();
							//html.set
						}
					}
				}
			}

		});

	}

}